//
//  SYTabBar.swift
//  SYWeibo
//
//  Created by Saya on 16/4/6.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit

class SYTabBar: UITabBar {
    //添加plus按键,代码创建控件的时候底成会调用这个方法
    override init(frame: CGRect) {
        super.init(frame:frame)
        addSubview(plusBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //重新设置frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //遍历子视图在获取控件
        let width = UIScreen.mainScreen().bounds.width / 5
        let height = bounds.height
        let fristRect = CGRect(x: 0, y: 0, width: width, height: height)
        var index:CGFloat = 0
        for subView in subviews{
//            print(subView)
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subView.frame = CGRectOffset(fristRect, index*width, 0)
                index += (index==1) ? 2 : 1
            }
        }
        
        //设置plus按键的frame
        plusBtn.frame = CGRectOffset(fristRect, 2*width, 0)
    }

    //懒加载plus按键
    lazy var plusBtn :UIButton = {
        let temBtn = UIButton()
        temBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        temBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        temBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        temBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        return temBtn
    }()
}
